package com.apes.fn.server.vehicleFile.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.fn.crm.customerExpand.FnCustomerExpand;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.swing.*;
import java.util.Date;

/**
 * @program: apes-server
 * @description: 车辆档案上传信息
 * @author: czs
 * @create: 2023-11-02 11:27
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = VehicleFileUploadInfo.CONST_TABLE_NAME,indexes = {
        @Index(columnList = "car_license"),
        @Index(columnList = "order_id")
})
public class VehicleFileUploadInfo extends Model {
    public static final String CONST_TABLE_NAME = "fn_vehicle_file_upload_info";

    @Id
    @Column(name = "id", nullable = false, length = 24)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "车辆档案上传信息编码", padding = 12)
    private String id;

    @Comment("部门")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
    private Dept dept;

    @Column(name = "order_id", length = 32,nullable = false)
    @Comment("单据编码")
    private String orderId;

    @Column(name = "order_type", length = 32,nullable = false)
    @Comment("单据类型")
    private String orderType;

    @Column(name = "car_license", length = 32,nullable = false)
    @Comment("车牌号码")
    private String carLicense;

    @Column(name = "real_name", length = 32)
    @Comment("车主姓名")
    private String realName;
    @Column(name = "serve_types", length = 256)
    @Comment("服务项目")
    private String serveTypes;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fn_customer_expand_id", referencedColumnName = "id")
    @Comment("车辆扩展表")
    private FnCustomerExpand fnCustomerExpand;

    @Column(name = "receivable_date")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Comment("收款时间")
    private Date receivableDate;

    @Comment("实收金额")
    @Column(name = "actual_total", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double actualAmount = 0;  //实收金额

    @Column(name = "upload_date")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Comment("上传时间")
    private Date uploadDate;

    @Comment("上传状态")
    @Column(name = "is_uploaded", nullable = false)
    private boolean uploaded = false;

    @Comment("上传结果")
    @Column(name = "upload_msg", length = 256)
    private String uploadMsg ;

    @Comment("上传平台")
    @Column(name = "upload_platform", length = 36)
    private String uploadPlatform ;

    @Transient
    private String receivableDateStr;

    @Transient
    private String uploadMsgFormat;

    public String getUploadMsgFormat() {
        if (this.uploadMsg ==null)return "--";
        return uploadMsg;
    }

    public String getReceivableDateStr() {
        if (this.receivableDate ==null)return "--";
        return DateUtil.format(receivableDate, "yyyy.MM.dd HH:mm:ss");
    }
    @Transient
    private String uploadDateStr;

    public String getUploadDateStr() {
        if (this.uploadDate ==null)return "--";
        return DateUtil.format(uploadDate, "yyyy.MM.dd HH:mm:ss");
    }

    @Transient
    private String creatorName;

    public String getCreatorName() {
        if (getCreator()==null)return "--";
        PartyRole partyRole = SpringManager.getBean(PartyRoleRepository.class).findOne(getCreator());
        return partyRole.getName();
    }


}
